<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>
    <el-steps :active="3" align-center finish-status="success">
      <el-step title="步骤1" description="选择商品类别"></el-step>
      <el-step title="步骤2" description="填写商品的基本信息"></el-step>
      <el-step title="步骤3" description="选择商品相册"></el-step>
    </el-steps>
    <el-divider/>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="基本信息">
        <el-descriptions :title="ruleForm.name">
          <el-descriptions-item label="品牌">
            <el-tag size="small">{{ ruleForm.brandName }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类别" span="2">{{ ruleForm.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="标题" span="3">{{ ruleForm.title }}</el-descriptions-item>
          <el-descriptions-item label="简介" span="3">{{ ruleForm.description }}</el-descriptions-item>
          <el-descriptions-item label="相册" span="3">{{ ruleForm.albumName }}</el-descriptions-item>
          <el-descriptions-item label="货号">{{ ruleForm.typeNumber }}</el-descriptions-item>
          <el-descriptions-item label="售价">{{ ruleForm.listPrice }}</el-descriptions-item>
          <el-descriptions-item label="计件单位">{{ ruleForm.unit }}</el-descriptions-item>
          <el-descriptions-item label="库存">{{ ruleForm.stock }}</el-descriptions-item>
          <el-descriptions-item label="库存阈警值">{{ ruleForm.stockThreshold }}</el-descriptions-item>
          <el-descriptions-item label="排序序号">{{ ruleForm.sort }}</el-descriptions-item>
          <el-descriptions-item label="关键词">{{ ruleForm.keywords }}</el-descriptions-item>
          <el-descriptions-item label="标签">{{ ruleForm.tags }}</el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item label="商品详情">
        <div id="WangEditor"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      editor: {},
      ruleForm: {},
      rules: {}
    }
  },
  methods: {
    backToPreStep() {
      // 返回上一步
      this.$router.push('/sys-admin/product/spu/add-new-3');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      if (!localRuleFormString) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
      }
      let localRuleForm = JSON.parse(localRuleFormString);
      console.log('第三步传过来的数据 = ' + localRuleForm)
      if (!localRuleForm.categoryId) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
      } else if (!localRuleForm.brandId) {
        this.$router.push('/sys-admin/product/spu/add-new-2');
      } else if (!localRuleForm.albumId) {
        this.$router.push('/sys-admin/product/spu/add-new-3');
      }
      this.ruleForm = localRuleForm;
    },
    initWangEditor() {
      this.editor = new this.wangEditor('#WangEditor');
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    submitForm(formName) {
      // 执行全部的检查规则
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //
          let detail = this.editor.txt.html();
          this.ruleForm.detail = detail;
          console.log('即将提交给服务器的数据 = ')
          console.log(this.ruleForm);
          // 链接后端服务器的接口
          let url = 'http://localhost:9080/spus/add-new';
          // 打印url变量的数值
          console.log("url=" + url);
          // 使用axios发送POST请求到url指定的地址
          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData)
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 2000) {
              this.$alert('添加SPU成功', '提示', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$router.push('/sys-admin/product/spu/add-new-1')
                  console.log(action);
                }
              });
              // alert('添加SPU成功!')
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.initWangEditor();
  }
}

/*
 * [关于富文本编辑器[wangeditor的使用]
 * 1. 安装命令
 *    npm i wangeditor -S
 * 2. 在main.js中进行配置:
 *    import WangEditor from 'wangeditor'
 *    Vue.prototype.WangEditor = WangEditor
 * 3. 在视图组件中使用
 *    3.1 在设计页面是,使用任何容器标签,添加ID,表示此标签适用于富文本编辑器的区域
 *        例如:<div id="WangEditor"></div>
 *    3.2 需要在页面刚刚加载时,初始化此富文本编辑器
 *        例如:new this.wangEditor('#WangEditor').create();[不推荐]
 *        更建议将创建出来的对象复赋值到变量上,方便后期调用
 *        则应该在data中返回时申明的editor属性
 *        并且:this.editor = new this.wangEditor('#WangEditor');  this.editor.create();
 */
</script>
<style>

</style>